package com.lyn.module_custom_base.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

/**
 * @author:longyongning
 * @date:2021/10/2
 * @description:
 */
public abstract class BaseDialog<B extends ViewBinding> extends Dialog {

    private boolean dispatchKeyEvent = false;

    private boolean customLengthAndWidth = false;

    public B binding;

    public BaseDialog(@NonNull Context context) {
        super(context);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public void setDispatchKeyEvent(boolean dispatchKeyEvent) {
        this.dispatchKeyEvent = dispatchKeyEvent;
    }

    public void setCustomLengthAndWidth(boolean customLengthAndWidth) {
        this.customLengthAndWidth = customLengthAndWidth;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        onWindowFocusChanged(true);
        binding = getViewBinding();
        setContentView(binding.getRoot());
        bindData(binding, this);
        bindListener(binding, this);
        dispatchKeyEvent = getDispatchKeyEvent();
        customLengthAndWidth = customLengthAndWidth();
        if (customLengthAndWidth) {
            Window window = this.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.setGravity(Gravity.CENTER);
            //获取屏幕宽度
            WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
            lp.height = wm.getDefaultDisplay().getHeight() / 10 * 6;
            lp.width = wm.getDefaultDisplay().getWidth() / 10 * 8;
            window.setAttributes(lp);
        }

    }


    protected abstract B getViewBinding();

    protected abstract void bindData(final B binding, final Dialog dialog);

    protected abstract void bindListener(final B binding, final Dialog dialog);

    protected abstract boolean getDispatchKeyEvent();

    protected abstract boolean customLengthAndWidth();

    @Override
    public void show() {
        super.show();
        DialogManager.getInstance().addDialog(this);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        DialogManager.getInstance().dismissDialog(this);
    }

    @Override
    public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
        if (!dispatchKeyEvent) {
            return false;
        }
        return super.dispatchKeyEvent(event);
    }

}

